﻿Ext.define('app.prm.detail.East', {
    extend: 'Ext.panel.Panel',
    region: 'east',
    split: false,
    width: 300,
    border: true,
    autoScroll: true,
    projectId: 0,
    //bodyStyle: 'padding:7px 0 0 0',
    //layout: 'accordion',
    //layoutConfig: {
    //    activeOnTop: true,  //设置打开的字面板置顶  
    //    collapseFirst: false,
    //    fill: true,         //字面板充满父面板的剩余空间  
    //    hideCollapseTool: false,   //显示“展开收缩”按钮  
    //    titleCollapse: true,      //允许通过点击子面板的标题来展开或者收缩面板  
    //    animate: true             //使用动画效果  
    //},
    initComponent: function () {
        var me = this;
        me.items = [me.getProjectStatus(), me.getProjectInfo(), me.getProjectCost(), me.getProjectTeam()];
        me.callParent();
    },
    listeners: {
        afterrender: function (me) {
            me.initStatus();
            me.initTeam();
        }
    },
    getProjectStatus: function () {
        var me = this;
        if (!this.prmStatus) {
            this.prmStatus = new Ext.form.Panel({
                defaultType: 'displayfield',
                bodyStyle: 'padding:10px',
                border: true,
                title: '项目状态',
                //collapsed: false,
                //collapsible: false,
                tools: [
                {
                    type: 'gear',
                    tooltip: '编辑',
                    callback: function (panel, tool, event) {
                        var win = Ext.create("app.prm.detail.EditProgress", { projectId: me.projectId });
                        win.on("savesuccess", function (formValues) {
                            me.prmStatus.getForm().setValues(formValues);
                            win.close();
                        });
                        win.show();
                    }
                }],
                items: [
                    { fieldLabel: '项目状态', name: 'status' },
                    //{ fieldLabel: '进度状态'},
                    { fieldLabel: '任务描述', name: 'task' },
                    { fieldLabel: '进展描述', name: 'note' }
                ]
            });
        }
        return this.prmStatus;
    },
    getProjectInfo: function () {
        var me = this;
        if (!this.prmInfo) {
            this.prmInfo = new Ext.form.Panel({
                defaultType: 'displayfield',
                bodyStyle: 'padding:10px',
                border: true,
                title: '基本信息',
                //  collapsible: true,
                //collapsed: false,
                tools: [
                {
                    type: 'gear',
                    tooltip: '编辑',
                    callback: 'onEditInfo'
                }],
                items: [
                    { fieldLabel: '项目编号', name: 'projectNo', bind: { value: '{projectNo}' } },
                    { fieldLabel: '项目名称', name: 'projectName', bind: { value: '{projectName}' } },
                    { fieldLabel: '中试地点', name: 'address', bind: { value: '{address}' } },
                    { fieldLabel: '产品号', name: 'productNo', bind: { value: '{productNo}' } },
                    { fieldLabel: '客户', name: 'customer', bind: { value: '{customer}' } },
                    { fieldLabel: '研发中心', name: 'rdCenter', bind: { value: '{rdCenter}' } },
                    { fieldLabel: '研究室', name: 'lab', bind: { value: '{lab}' } },
                    { fieldLabel: '客户编号', name: 'customeCode', bind: { value: '{customeCode}' } },
                    { fieldLabel: '质量标准', name: 'qualityStandard', bind: { value: '{qualityStandard}' } },
                    { fieldLabel: '项目性质', name: 'projectNature', bind: { value: '{projectNature}' } },
                    { fieldLabel: '备注', name: 'remark', bind: { value: '{remark}' } }
                ]
            });
        }
        return this.prmInfo;
    },
    getProjectCost: function () {
        var me = this;
        if (!this.prmCost) {
            this.prmCost = new Ext.form.Panel({
                defaultType: 'displayfield',
                bodyStyle: 'padding:10px',
                border: true,
                title: '项目成本',
                // collapsible: true,
                //collapsed: false,
                tools: [
                {
                    type: 'gear',
                    tooltip: '编辑',
                    callback: 'onEditCose'
                }],
                items: [
                    { fieldLabel: '实际项目成本', name: 'actualCost', bind: { value: '{actualCost}' } },
                    { fieldLabel: '实际人力成本', name: 'actualWorkCost', bind: { value: '{actualWorkCost}' } },
                    { fieldLabel: '计划人力成本', name: 'targetWorkCost', bind: { value: '{targetWorkCost}' } },
                    { fieldLabel: '实际完成量', name: 'actualOutPut', bind: { value: '{actualOutPut}' } },
                    { fieldLabel: '实际原料成本', name: 'actualMaterialCost', bind: { value: '{actualMaterialCost}' } },
                    { fieldLabel: '目标项目成本', name: 'targetCost', bind: { value: '{targetCost}' } },
                    { fieldLabel: '目标完成量', name: 'targetOutPut', bind: { value: '{targetOutPut}' } },
                    { fieldLabel: '目标原料成本', name: 'targetMaterialCost', bind: { value: '{targetMaterialCost}' } }
                ]
            });
        }
        return this.prmCost;
    },
    initStatus: function ()
    {
        var me = this;
        App.AjaxGet('/api/services/app/project/GetLastProgressOutPut?projectId=' + me.projectId, function (data) {
            console.log("---------------------------------------");
            console.log(data);
            console.log("---------------------------------------");
            me.prmStatus.getForm().setValues(data.result);
        });
    },
    getProjectTeam: function () {
        var me = this;
        if (!this.prmTeam) {
            this.prmTeam = new Ext.panel.Panel({
                bodyStyle: 'padding:10px',
                border: true,
                title: '项目成员',
                //collapsible: true,
                //collapsed: true,
                tools: [
                {
                    type: 'gear',
                    tooltip: '编辑',
                    callback: function (panel, tool, event) {
                        var win = Ext.create("app.prm.detail.SelectTeam", { title: '编辑成员', projectId: me.projectId });
                        win.on("savesuccess", function () {
                            me.initTeam();
                            win.close();
                        });
                        win.show();
                    }
                }],
                items: []
            });
        }
        return this.prmTeam;
    },
    initTeam: function () {
        var me = this;
        App.AjaxGet("/api/services/app/project/GetProjectTeam?projectId=" + me.projectId, function (data) {
            me.getProjectTeam().removeAll(true);
            console.log(data);
            for (var i = 0; i < data.result.length; i++) {
                var record = data.result[i];
                var panel = new Ext.panel.Panel({
                    border: false,
                    style: 'margin:0 0 10px 0',
                    items: [
                        { border: false, html: '<b>' + record.roleName + '</b>' },
                    ]
                });

                for (var j = 0; j < record.teams.length; j++) {
                    var team = record.teams[j];
                    panel.add(new Ext.panel.Panel({
                        border: false,
                        html: '<a style="text-decoration: underline;cursor:pointer">' + team.value + '</a>',
                        listeners: {
                            afterrender: function (component) {
                                component.getEl().on('click', function () {
                                    Ext.Msg.confirm('系统提示', '是否确认删除?', function (btn) {
                                        if (btn == 'yes') {
                                            me.delPrmRole(team.key, me.projectId);
                                        }
                                    });
                                });
                            }
                        }
                    }));
                }
                me.getProjectTeam().add(panel);
            }
        });
    },
    delPrmRole: function (projectRoleId,projectId) {
        var me = this;
        App.AjaxDelete('/api/services/app/project/RemoveProjectTeam?projectRoleId=' + projectRoleId + "&projectId=" + projectId, function () {
            me.initTeam();
        });
    }
})